﻿using System;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace ClownStopper
{

    public interface ITextMessage: IMessage
    {
        String Text
        {
            get;
            set;
        }
        string ClientIp { get; set; }

    }


    public class TextMessage : ITextMessage
    {

        public /*MessageType*/int Type
        {
            get { return (int)MessageType.TextDataMessage; }
        }

        public Byte[] GetMessagePacket()
        {
            String textResult = "";
            textResult += "<message>";

            textResult += "<type>" + Type + "</type>";
            textResult += "<text>" + Text + "</text>";
            textResult += "<clientip>" + ClientIp + "</clientip>";

            textResult += "</message>";

            return Encoding.UTF8.GetBytes(textResult);
        }



        public String Text { get; set; }

        public String ClientIp { get; set; }

        public bool Parse(Byte[] data, out string error)
        {
            error = null;
            //parse the message from "incoming data packet"
            String messageContent = Encoding.UTF8.GetString(data);
            //////////////////////////////////////////////////////////////////////////
            //Data validation
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.LoadXml(messageContent);
            }
            catch (XmlException ex)
            {
                error = "There was an xml parsing error in the received message : " + ex.Message;
                return false;
            }
            catch (Exception ex)
            {
                error = "There was an error in the received message : " + ex.Message;
                return false;
            }

            MessageType type = MessageType.EmptyMessage;

            XmlElement messageElement = xmlDoc.DocumentElement;
            if (messageElement != null)
                if (messageElement.Name == "message")
                {
                    foreach (XmlNode node in messageElement.ChildNodes)
                    {
                        if (node.Name == "type")
                        {
                            type = (MessageType)Enum.Parse(typeof(MessageType), node.InnerText);
                            break;
                        }
                    }
                }

            if (type != MessageType.TextDataMessage)
            {
                error = "TextMessage: The supplied data was the wrong message type!";
                return false;
            }

            //////////////////////////////////////////////////////////////////////////
            // The real data parsing
            Text = "";

            if (messageElement != null)
                foreach (XmlNode node in messageElement.ChildNodes)
                {
                    if (node.Name == "text")
                    {
                        Text = node.InnerText;
                    }
                    if (node.Name == "clientip")
                    {
                        ClientIp = node.InnerText;
                    }
               
                }

            //////////////////////////////////////////////////////////////////////////
            return true;
        }

        public IMessage Clone()
        {
            var result = new TextMessage {Text = Text, ClientIp = ClientIp};
            return result;
        }
    }
}
